<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- <el-input v-model="orderReqDto.phone" placeholder="手机号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" /> -->
      <el-button v-waves class="filter-item" type="success" icon="el-icon-insert" @click="handleCreate">
        新增
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="编号" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" min-width="100">
        <template slot-scope="{row}">
          <div v-if=" row.type==1">会员</div>
          <div v-if="row.type==0">非会员</div>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" min-width="100">
        <template slot-scope="{row}">
          {{ row.question }}
        </template>
      </el-table-column>
      <el-table-column label="内容" min-width="200" align="center">
        <template slot-scope="{row}">
          {{ row.answer }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="320" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="orderReqDto.current"
      :limit.sync="orderReqDto.size"
      @pagination="urlProb"
    />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="replyDto" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <el-form-item label="类型" prop="type1">
          <el-select v-model="replyDto.type" placeholder="请选择会员类型">
            <el-option label="会员" :value="1"></el-option>
            <el-option label="非会员" :value="0"></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="标题" prop="username">
          <el-input v-model="replyDto.title" />
        </el-form-item>

        <el-form-item label="内容" prop="account">
          <textarea v-model="replyDto.content" style="width:100%;height:400px;" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="quxiao">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateUrlProb()">
          确认
        </el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { urlProb, insertUrlProb, updateUrlProb, deleteUrlProb } from '@/api/service'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'Service',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      msgs: [],
      tableData: [],
      total: 0,
      tableKey: 0,
      orderReqDto: {
        size: 10,
        current: 1,
        all: true,
      },
      textMap: {
        update: '编辑',
        create: '新增'
      },
      countOrder: {},
      dialogFormVisible: false,
      editdialog: false,
      replyDto: {
        id: 0,
        title: '',
        content: '',
        type:0
      },
      dialogStatus: '',
      dialogPvVisible: false,
      listLoading: false,
      value: ''
    }
  },
  created() {
    this.urlProb()
  },
  methods: {
    quxiao() {
        this.dialogFormVisible = false
        this.urlProb()
    },
    handleUpdate(row) {
      this.replyDto.id = row.id
      this.replyDto.title = row.question
      this.replyDto.content = row.answer
      this.replyDto.type = row.type
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    updateUrlProb() {
      updateUrlProb(this.replyDto).then(res => {
        if (res.data.code === 200) {
          this.$notify({
            title: 'Success',
            message: '更新成功',
            type: 'success',
            duration: 2000
          })
          this.dialogFormVisible = false
          this.urlProb()
        }
      })
    },
    handleCreate() {
      this.replyDto = {}
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    createData() {
      insertUrlProb(this.replyDto).then(res => {
        if (res.data.code === 200) {
          this.$notify({
            title: 'Success',
            message: '新增成功',
            type: 'success',
            duration: 2000
          })
          this.dialogFormVisible = false
          this.urlProb()
        }
      })
    },
    handleDelete(row) {
      deleteUrlProb({ id: row.id }).then(res => {
        if (res.data.code === 200) {
          this.$notify({
            title: 'Success',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.dialogFormVisible = false
          this.urlProb()
        }
      })
    },
    dataFilter(val) {
      this.value = val
      if (val) { // val存在
        this.options = this.optionsCopy.filter((item) => {
          if (!!~item.channel_name.indexOf(val) || !!~item.channel_name.toUpperCase().indexOf(val.toUpperCase())) {
            return true
          }
        })
      } else { // val为空时，还原数组
        this.options = this.optionsCopy
      }
    },
    async insertUrlProb(row) {
      const res = await insertUrlProb({ id: row.id })
      this.msgs = res.data.data
      this.dialogFormVisible = true
    },
    // 获取订单列表
    async urlProb(orderReqDto) {
      const res = await urlProb(this.orderReqDto)
      if (res.data.data.records === null) {
        this.tableData = null
      } else {
        this.tableData = res.data.data.records
        this.total = res.data.data.total
      }
    },

    handleFilter() {
      this.orderReqDto.current = 1
      this.orderReqDto.all = false
      this.urlProb(this.orderReqDto)
    },
    cancel() {
      this.editdialog = false
    },
    handlereply(row) {
      this.replyDto.id = row.id
      this.replyDto.replyContent = ''
      this.editdialog = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    }
  }
}
</script>

<style scoped>
.traces{
  overflow:hidden;
}
.left{
  overflow:hidden;
  margin-left: 15px;
  margin-top: 10px;
  width: 100%;
}
.leftcontent{
  word-wrap:break-word;
  padding-left: 10px;
  padding-right: 10px;
  margin-left: 10px;
  border-radius: 5px;
  color: #666666;
  background-color: #EBEBEB;
  width: 235px;
  float: left;
  text-align:left;
  line-height: 40px;
}
.divimg{
  width: 28px;
}
.leftimg{
  display: block;
  float: left;
  width: 38px;
  border-radius: 50%;
}
.rightimg{
  margin-right: 15px;
  display: block;
  float: right;
  width: 38px;
  border-radius: 50%;
}
.right{
  overflow:hidden;
  padding-right: 10px;
  margin-top: 10px;
  width: 100%;
}
.rightcontent{
  word-wrap:break-word;
  padding-left: 10px;
  padding-right: 10px;
  margin-right: 10px;
  border-radius: 5px;
  background-color: #F6AC02;
  color: white;
  width: 235px;
  float: right;
  text-align:left;
  line-height: 40px;
}
.test1::-webkit-scrollbar {
 width: 0;
}
 .test1::-webkit-scrollbar-track {
 background-color:white;
 -webkit-border-radius: 2em;
 -moz-border-radius: 2em;
 border-radius:2em;
}
 .test1::-webkit-scrollbar-thumb {
 background-color:white;
 -webkit-border-radius: 2em;
 -moz-border-radius: 2em;
 border-radius:2em;
}
</style>
